using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;

using BPMN.MainWindow;

namespace BPMN.MainWindow
{
    public partial class UCLaneProperties : UserControl, IUpdateOnEvent
    {
        FigureEvent eventMethod;
        FigureParentChild currentFigure = null;
        WindowMain windowMain;
        #region IUpdateOnEvent Members

        public UCLaneProperties()
        {
            InitializeComponent();
            eventMethod = new FigureEvent(update);
            RegisterForReceivingUpdates();
            textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
        }
        public void SetParent(WindowMain windowMain)
        {
            this.windowMain = windowMain;
        }
        void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (currentFigure != null)
                {
                    if (textBox1.Text != "")
                    {
                        windowMain.BPMNClass.Pools[currentFigure.Name].Lanes[currentFigure.Child.Name].Name = textBox1.Text;
                        FigureParentChild fpc2 = new FigureParentChild(textBox1.Text, null, FigureType.Lane);
                        FigureParentChild fpc1 = new FigureParentChild(currentFigure.Name, fpc2, FigureType.Pool);
                        EventManagement.SendEventSignal(this, new FigureUpdateEventArgs(currentFigure, new FigureParentChild(currentFigure.Name, fpc2, FigureType.Pool), FigureEventType.Modified, FigureType.Lane));
                        currentFigure = new FigureParentChild(currentFigure.Name, fpc2, FigureType.Pool);
                    }
                    else
                    {
                        MessageBox.Show("Name cannot be empty", "Error", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    }
                }
            }
        }

        public void RegisterForReceivingUpdates()
        {
            EventManagement.GetInstance().RegisterNewModule(FigureType.Lane,new ModuleEventStruct( this, eventMethod));
        }

        public void update(object sender, FigureUpdateEventArgs args)
        {
            if (sender != null && args != null)
            {
                switch (args.Type)
                {
                    case FigureEventType.Selection:
                        switch (args.FigureType)
                        {
                            case FigureType.Lane:
                                currentFigure = args.NewFigure;
                                textBox1.Text = args.NewFigure.Name;
                                break;
                        }
                        break;
                    case FigureEventType.Modified:

                        break;
                }


            }
        }
        #endregion
    }
}
